<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 JSON 数据解析</h1>
        <p>JSON (JavaScript Object Notation)  是一种轻量级的数据交换格式。它基于ECMAScript的一个子集。</p>
        <p>Python3 中可以使用 json 模块来对 JSON 数据进行编解码，它包含了两个函数：</p>
        <ul>
            <li><b>json.dumps(): </b>对数据进行编码。</li>
            <li><b>json.loads(): </b>对数据进行解码。</li>
        </ul>
        <p>
            在json的编解码过程中，python 的原始类型与json类型会相互转换，具体的转化对照如下：</p>
        <h3>Python 编码为 JSON 类型转换对应表：</h3>
        <table border="1" class="reference">
            <colgroup>
                <col width="73%">
                <col width="27%">
            </colgroup>
            <thead valign="bottom">
            <tr class="row-odd"><th class="head">Python</th>
                <th class="head">JSON</th>
            </tr>
            </thead>
            <tbody valign="top">
            <tr class="row-even"><td>dict</td>
                <td>object</td>
            </tr>
            <tr class="row-odd"><td>list, tuple</td>
                <td>array</td>
            </tr>
            <tr class="row-even"><td>str</td>
                <td>string</td>
            </tr>
            <tr class="row-odd"><td>int, float, int- &amp; float-derived Enums</td>
                <td>number</td>
            </tr>
            <tr class="row-even"><td>True</td>
                <td>true</td>
            </tr>
            <tr class="row-odd"><td>False</td>
                <td>false</td>
            </tr>
            <tr class="row-even"><td>None</td>
                <td>null</td>
            </tr>
            </tbody>
        </table>

        <h3>JSON 解码为 Python 类型转换对应表：</h3>
        <table border="1" class="reference">
            <colgroup>
                <col width="44%">
                <col width="56%">
            </colgroup>
            <thead valign="bottom">
            <tr class="row-odd"><th class="head">JSON</th>
                <th class="head">Python</th>
            </tr>
            </thead>
            <tbody valign="top">
            <tr class="row-even"><td>object</td>
                <td>dict</td>
            </tr>
            <tr class="row-odd"><td>array</td>
                <td>list</td>
            </tr>
            <tr class="row-even"><td>string</td>
                <td>str</td>
            </tr>
            <tr class="row-odd"><td>number (int)</td>
                <td>int</td>
            </tr>
            <tr class="row-even"><td>number (real)</td>
                <td>float</td>
            </tr>
            <tr class="row-odd"><td>true</td>
                <td>True</td>
            </tr>
            <tr class="row-even"><td>false</td>
                <td>False</td>
            </tr>
            <tr class="row-odd"><td>null</td>
                <td>None</td>
            </tr>
            </tbody>
        </table>
        <h3>json.dumps 与 json.loads 实例</h3>
        <p>以下实例演示了 Python 数据结构转换为JSON：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> json

</span><span class="com"># Python 字典类型转换为 JSON 对象</span><span class="pln">
data </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="str">'no'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'name'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">'Runoob'</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'url'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">'http://www.runoob.com'</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

json_str </span><span class="pun">=</span><span class="pln"> json</span><span class="pun">.</span><span class="pln">dumps</span><span class="pun">(</span><span class="pln">data</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Python 原始数据："</span><span class="pun">,</span><span class="pln"> repr</span><span class="pun">(</span><span class="pln">data</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"JSON 对象："</span><span class="pun">,</span><span class="pln"> json_str</span><span class="pun">)</span></pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Python</span><span class="pln"> </span><span class="pun">原始数据：</span><span class="pln"> </span><span class="pun">{</span><span class="str">'url'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'http://www.runoob.com'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'no'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="str">'name'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Runoob'</span><span class="pun">}</span><span class="pln">
JSON </span><span class="pun">对象：</span><span class="pln"> </span><span class="pun">{</span><span class="str">"url"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"http://www.runoob.com"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"no"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="str">"name"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Runoob"</span><span class="pun">}</span></pre>
        <p>通过输出的结果可以看出，简单类型通过编码后跟其原始的repr()输出结果非常相似。</p>
        <p>接着以上实例，我们可以将一个JSON编码的字符串转换回一个Python数据结构：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> json

</span><span class="com"># Python 字典类型转换为 JSON 对象</span><span class="pln">
data1 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="str">'no'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'name'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">'Runoob'</span><span class="pun">,</span><span class="pln">
    </span><span class="str">'url'</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="str">'http://www.runoob.com'</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

json_str </span><span class="pun">=</span><span class="pln"> json</span><span class="pun">.</span><span class="pln">dumps</span><span class="pun">(</span><span class="pln">data1</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"Python 原始数据："</span><span class="pun">,</span><span class="pln"> repr</span><span class="pun">(</span><span class="pln">data1</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"JSON 对象："</span><span class="pun">,</span><span class="pln"> json_str</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 将 JSON 对象转换为 Python 字典</span><span class="pln">
data2 </span><span class="pun">=</span><span class="pln"> json</span><span class="pun">.</span><span class="pln">loads</span><span class="pun">(</span><span class="pln">json_str</span><span class="pun">)</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"data2['name']: "</span><span class="pun">,</span><span class="pln"> data2</span><span class="pun">[</span><span class="str">'name'</span><span class="pun">])</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"data2['url']: "</span><span class="pun">,</span><span class="pln"> data2</span><span class="pun">[</span><span class="str">'url'</span><span class="pun">])</span></pre>
        <p>执行以上代码输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Python</span><span class="pln"> </span><span class="pun">原始数据：</span><span class="pln"> </span><span class="pun">{</span><span class="str">'name'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'no'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="str">'url'</span><span class="pun">:</span><span class="pln"> </span><span class="str">'http://www.runoob.com'</span><span class="pun">}</span><span class="pln">
JSON </span><span class="pun">对象：</span><span class="pln"> </span><span class="pun">{</span><span class="str">"name"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Runoob"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"no"</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="str">"url"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"http://www.runoob.com"</span><span class="pun">}</span><span class="pln">
data2</span><span class="pun">[</span><span class="str">'name'</span><span class="pun">]:</span><span class="pln">  </span><span class="typ">Runoob</span><span class="pln">
data2</span><span class="pun">[</span><span class="str">'url'</span><span class="pun">]:</span><span class="pln">  http</span><span class="pun">:</span><span class="com">//www.runoob.com</span></pre>
        <p>如果你要处理的是文件而不是字符串，你可以使用 <b>json.dump()</b> 和 <b>json.load()</b> 来编码和解码JSON数据。例如：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com"># 写入 JSON 数据</span><span class="pln">
</span><span class="kwd">with</span><span class="pln"> open</span><span class="pun">(</span><span class="str">'data.json'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'w'</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> f</span><span class="pun">:</span><span class="pln">
    json</span><span class="pun">.</span><span class="kwd">dump</span><span class="pun">(</span><span class="pln">data</span><span class="pun">,</span><span class="pln"> f</span><span class="pun">)</span><span class="pln">

</span><span class="com"># 读取数据</span><span class="pln">
</span><span class="kwd">with</span><span class="pln"> open</span><span class="pun">(</span><span class="str">'data.json'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'r'</span><span class="pun">)</span><span class="pln"> </span><span class="kwd">as</span><span class="pln"> f</span><span class="pun">:</span><span class="pln">
    data </span><span class="pun">=</span><span class="pln"> json</span><span class="pun">.</span><span class="pln">load</span><span class="pun">(</span><span class="pln">f</span><span class="pun">)</span></pre>
        <p>更多资料请参考：<a href="https://docs.python.org/3/library/json.html" target="_blank">https://docs.python.org/3/library/json.html</a>

        </p></div>
</div>
</body>
</html>
